<html>
<head><meta charset="utf-8"><title>const Vec::new() and imut Vec::new() behave differently · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html">const Vec::new() and imut Vec::new() behave differently</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="192458804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192458804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192458804">(Mar 31 2020 at 22:01)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/54475#issuecomment-606895030" title="https://github.com/rust-lang/rust/issues/54475#issuecomment-606895030">https://github.com/rust-lang/rust/issues/54475#issuecomment-606895030</a></p>
<p>I thought that rustc was sort of promoting immutable variables to const by itself or did I misunderstand?<br>
It seems to me that<br>
<code>let x: Vec&lt;u8&gt; = Vec::new();</code> and<br>
<code>const x: Vec&lt;u8&gt; = Vec::new();</code> should behave the same?</p>



<a name="192471797"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192471797" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192471797">(Apr 01 2020 at 00:46)</a>:</h4>
<p>no, the promotion is of borrows of constant expressions</p>



<a name="192471815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192471815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192471815">(Apr 01 2020 at 00:47)</a>:</h4>
<p>like <code>&amp;false</code> has type <code>&amp;'static bool</code></p>



<a name="192471915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192471915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192471915">(Apr 01 2020 at 00:48)</a>:</h4>
<p><code>const fn</code>s called from runtime <code>fn</code>s still run at runtime</p>



<a name="192492612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192492612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192492612">(Apr 01 2020 at 07:41)</a>:</h4>
<p>in particular, this implicit promotion does <em>not</em> happen for function calls (with a few exceptions purely for backwards-compatibility)</p>



<a name="192492710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192492710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192492710">(Apr 01 2020 at 07:42)</a>:</h4>
<p><span class="user-mention" data-user-id="217864">@matthiaskrgr</span> see <a href="https://github.com/rust-lang/const-eval/blob/master/promotion.md" title="https://github.com/rust-lang/const-eval/blob/master/promotion.md">https://github.com/rust-lang/const-eval/blob/master/promotion.md</a> for some more details</p>



<a name="192492751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/const%20Vec%3A%3Anew%28%29%20and%20imut%20Vec%3A%3Anew%28%29%20behave%20differently/near/192492751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/const.20Vec.3A.3Anew().20and.20imut.20Vec.3A.3Anew().20behave.20differently.html#192492751">(Apr 01 2020 at 07:42)</a>:</h4>
<p>and also <a href="https://github.com/rust-lang/rfcs/blob/master/text/1414-rvalue_static_promotion.md" title="https://github.com/rust-lang/rfcs/blob/master/text/1414-rvalue_static_promotion.md">the original RFC</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>